﻿package fj.ui
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class FJButton extends MovieClip
	{
		public var hit:MovieClip;
		
		private var isOver:Boolean;
		private var isDown:Boolean;
		private var isSelected:Boolean;
		private var isDisabled:Boolean;

		public function FJButton()
		{
			stop();
			buttonMode = false;
			hit.buttonMode = true;
			hit.addEventListener( MouseEvent.MOUSE_OVER, hitOver);
			hit.addEventListener( MouseEvent.MOUSE_OUT, hitOut);
			hit.addEventListener( MouseEvent.MOUSE_DOWN, hitDown);
			hit.addEventListener( MouseEvent.MOUSE_UP, hitUp);
		}
		public function set selected( b:Boolean ):void
		{
			isSelected = b;
			if( isSelected ){
				
				gotoAndPlay( "selected:start" );
				addEventListener( Event.ENTER_FRAME, loopSelected );
				
				mouseEnabled = false;
				mouseChildren = false;
				hit.removeEventListener( MouseEvent.MOUSE_OVER, hitOver );
				hit.removeEventListener( MouseEvent.MOUSE_OUT, hitOut );
				hit.removeEventListener( MouseEvent.MOUSE_DOWN, hitDown );
				hit.removeEventListener( MouseEvent.MOUSE_UP, hitUp );
			} else {
				
				gotoAndPlay( "selected_stand:start" );
				addEventListener( Event.ENTER_FRAME, loopSelectedStand );
				
				mouseEnabled = true;
				mouseChildren = true;
				hit.addEventListener( MouseEvent.MOUSE_OVER, hitOver );
				hit.addEventListener( MouseEvent.MOUSE_OUT, hitOut );
				hit.addEventListener( MouseEvent.MOUSE_DOWN, hitDown );
				hit.addEventListener (MouseEvent.MOUSE_UP, hitUp );
			}
		}
		public function get selected():Boolean
		{
			return isSelected;
		}
		public function set disabled( b:Boolean ):void
		{
			isDisabled = b;
			if( isDisabled ){
				
				gotoAndPlay( "disabled:start" );
				addEventListener( Event.ENTER_FRAME, loopDisabled );
				
				mouseEnabled = false;
				mouseChildren = false;
				hit.removeEventListener( MouseEvent.MOUSE_OVER, hitOver );
				hit.removeEventListener( MouseEvent.MOUSE_OUT, hitOut );
				hit.removeEventListener( MouseEvent.MOUSE_DOWN, hitDown );
				hit.removeEventListener( MouseEvent.MOUSE_UP, hitUp );
			} else {
				
				gotoAndPlay( "disabled_stand:start" );
				addEventListener( Event.ENTER_FRAME, loopDisabledStand );
				
				mouseEnabled = true;
				mouseChildren = true;
				hit.addEventListener( MouseEvent.MOUSE_OVER, hitOver );
				hit.addEventListener( MouseEvent.MOUSE_OUT, hitOut );
				hit.addEventListener( MouseEvent.MOUSE_DOWN, hitDown );
				hit.addEventListener( MouseEvent.MOUSE_UP, hitUp );
			}
		}
		public function get disabled():Boolean
		{
			return isDisabled;
		}
		private function hitOver( e:MouseEvent )
		{
			isOver = true;
			gotoAndPlay( "over:start" );
			addEventListener( Event.ENTER_FRAME, loopOver );
		}
		private function hitOut( e:MouseEvent ):void
		{
			isOver = false;
			if ( isDown ) {
				gotoAndPlay( "up_stand:start" );
				addEventListener( Event.ENTER_FRAME, loopUpStand );
			} else {
				gotoAndPlay( "out:start" );
				addEventListener( Event.ENTER_FRAME, loopOut );
			}
			isDown = false;
		}
		private function hitDown( e:MouseEvent ):void
		{
			isDown = true;
			gotoAndPlay( "down:start" );
			addEventListener( Event.ENTER_FRAME, loopDown );
		}
		private function hitUp( e:MouseEvent ):void
		{
			isDown = false;
			gotoAndPlay( "up_over:start" );
			addEventListener( Event.ENTER_FRAME, loopUpOver );
			var evento:Event = new Event( "clicado" );
			dispatchEvent( evento );
		}
		private function loopOver(e :Event ):void
		{
			if ( currentLabel == "over:end" ) {
				stop();
				removeEventListener( Event.ENTER_FRAME, loopOver );
			}
		}
		private function loopOut( e:Event ):void
		{
			if ( currentLabel == "out:end" ) {
				gotoAndStop( "stand" );
				removeEventListener( Event.ENTER_FRAME, loopOut );
			}
		}
		private function loopDown( e:Event ):void
		{
			if ( currentLabel == "down:end" ) {
				stop();
				removeEventListener( Event.ENTER_FRAME, loopDown );
			}
		}
		private function loopUpStand( e:Event ):void
		{
			if ( currentLabel == "up_stand:end" ) {
				gotoAndStop( "stand" );
				removeEventListener( Event.ENTER_FRAME, loopUpStand );
			}
		}
		private function loopUpOver( e:Event ):void
		{
			if ( currentLabel == "up_over:end" ) {
				gotoAndStop( "over:end" );
				removeEventListener( Event.ENTER_FRAME, loopUpOver );
			}
		}
		private function loopSelected( e:Event ):void
		{
			if ( currentLabel == "selected:end" ) {
				stop()
				removeEventListener( Event.ENTER_FRAME, loopSelected );
			}
		}
		private function loopSelectedStand( e:Event ):void
		{
			if ( currentLabel == "selected_stand:end" ) {
				gotoAndStop( "stand" )
				removeEventListener( Event.ENTER_FRAME, loopSelectedStand );
			}
		}
		private function loopDisabled( e:Event ):void
		{
			if ( currentLabel == "disabled:end" ) {
				stop()
				removeEventListener( Event.ENTER_FRAME, loopDisabled );
			}
		}
		private function loopDisabledStand( e:Event ):void
		{
			if ( currentLabel == "disabled_stand:end" ) {
				gotoAndStop( "stand" );
				removeEventListener( Event.ENTER_FRAME, loopDisabledStand );
			}
		}
	}
}